--    Program: 		CAPARIMP_CUSTOMER_LOC_ASSIGNMENT.sql
--    Description:  Import AR CUSTOMER LOC ASSIGNMENT information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Kenny Leung
--    Date:         1-Feb-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
--whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp 			varchar2(2000);
                    		                 		
   cursor cur_ar_customer_loc_assignment is
      select * from CRST_HZ_LOC_ASSIGNMENTS;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
 
   for cur in cur_ar_customer_loc_assignment loop   	
     l_err_msg_tmp := '';
     
     begin
       -- import data
       insert into HZ_LOC_ASSIGNMENTS
       (
          LOCATION_ID             , 
          LOC_ID                  , 
          ORG_ID                  , 
          CREATED_BY              , 
          CREATION_DATE           , 
          LAST_UPDATE_LOGIN       , 
          LAST_UPDATE_DATE        , 
          LAST_UPDATED_BY         , 
          REQUEST_ID              , 
          PROGRAM_APPLICATION_ID  , 
          PROGRAM_ID              , 
          PROGRAM_UPDATE_DATE     , 
          WH_UPDATE_DATE          , 
          OBJECT_VERSION_NUMBER   , 
          CREATED_BY_MODULE       , 
          APPLICATION_ID            
       
       )
       values
       (
            CAPARIMP_MAPPING.GetHZLocationIDMapping(cur.LOCATION_ID),
            cur.LOC_ID                  ,
            CAPBRIMP_MAPPING.GetOrgMapping(cur.ORG_ID),
            CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
            cur.CREATION_DATE           ,
            cur.LAST_UPDATE_LOGIN       ,
            cur.LAST_UPDATE_DATE        ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.LAST_UPDATED_BY),
            cur.REQUEST_ID              ,
            cur.PROGRAM_APPLICATION_ID  ,
            cur.PROGRAM_ID              ,
            cur.PROGRAM_UPDATE_DATE     ,
            cur.WH_UPDATE_DATE          ,
            cur.OBJECT_VERSION_NUMBER   ,
            cur.CREATED_BY_MODULE       ,
            cur.APPLICATION_ID                            
       );  
     exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;	  	    
     
     if l_err_msg_tmp = '' then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;  	  
   end loop;
   
   if l_rejected_count <> 0 then
   	 commit;
   else
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPARIMP_CUSTOMER_LOCATION_ASSIGNMENT', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
